import { createStore } from 'vuex'
import cloneDeep from 'lodash/cloneDeep'
import common from './modules/common'
import user from './modules/user'

export default createStore({
  state: {
    count: 0,
    user: null,
    // 其他状态...
  },
  
  getters: {
    doubleCount: (state) => state.count * 2,
    isLoggedIn: (state) => !!state.user,
    // 其他getter...
  },
  
  mutations: {
    INCREMENT(state) {
      state.count++
    },
    SET_USER(state, user) {
      state.user = user
    },
    // 重置vuex本地储存状态
    resetStore (state) {
      Object.keys(state).forEach((key) => {
        state[key] = cloneDeep(window.SITE_CONFIG['storeState'][key])
      })
    }
  },
  
  actions: {
    increment({ commit }) {
      commit('INCREMENT')
    },
    async login({ commit }, { username, password }) {
      try {
        // 假设这是一个API调用
        const user = await api.login(username, password)
        commit('SET_USER', user)
        return user
      } catch (error) {
        console.error('Login failed:', error)
        throw error
      }
    },
    // 其他actions...
  },
  
  modules: {
    common,
    user
  },
  strict: process.env.NODE_ENV !== 'production'
})
